import json
import os
import time

from PyQt5.QtWidgets import QMainWindow, QTabWidget, QMenu, QAction, QFileDialog, QMessageBox
from PyQt5.uic import loadUi

from windows.MakeWindowClass import MakeWindow
from windows.PlayWindowClass import PlayWindow
from windows.SynthesisWindowClass import SynthesisWindow

"""
主窗口
"""


class HomeWindow:
    def __init__(self):
        self.window = loadUi('ui/home.ui')
        self.window: QMainWindow
        self.window.show()
        self.window.setWindowTitle('t2')

        # 当前工作路径
        self.this_workspace_dir = ''
        # 初始化配置
        self.init_read()

        self.window.tabWidget: QTabWidget
        # 声明子窗口
        self.play_window, self.synthesis_window, self.make_window = None, None, None
        # 初始化子窗口
        self.tabWidget_init_windows()

        """菜单栏"""
        menuBar = self.window.menuBar()
        menu_file = QMenu('文件', menuBar)
        menu_file.addAction('打开当前工作目录')
        menu_file.addAction('选择新的工作目录')
        menu_file.addAction('选择新的工作目录(默认创建)')
        menuBar.addMenu(menu_file)
        menuBar.triggered.connect(self.menuBar_triggered)

    def init_read(self):
        # 读取配置文件
        s = ''
        with open(file='config/global.json', mode='r', encoding='utf-8') as f:
            s = f.read()
        # 解析，初始化配置
        if s != '':
            value_dict = json.loads(s)
            self.this_workspace_dir = value_dict.get('this_workspace_dir')
        self.init_workspace()

    def tabWidget_init_windows(self):
        self.window.tabWidget: QTabWidget
        # 清除子窗口
        self.window.tabWidget.clear()
        self.play_window = PlayWindow(home_window=self)
        self.window.tabWidget.addTab(self.play_window.window, '音频播放')
        self.synthesis_window = SynthesisWindow(home_window=self)
        self.window.tabWidget.addTab(self.synthesis_window.window, '语音合成')
        self.make_window = MakeWindow(home_window=self)
        self.window.tabWidget.addTab(self.make_window.window, '音频处理')

    def menuBar_triggered(self, act: QAction):
        if act.text() == '打开当前工作目录':
            path = os.path.abspath(self.this_workspace_dir)
            os.system('explorer %s' % path)
        elif act.text() == '选择新的工作目录':
            dir_path = QFileDialog.getExistingDirectory(self.window, '选择新的工作目录', '')
            if dir_path != '':
                self.this_workspace_dir = dir_path
                self.init_workspace()
                self.tabWidget_init_windows()
        elif act.text() == '选择新的工作目录(默认创建)':
            self.this_workspace_dir = 'data/%s' % time.strftime('%Y%m%d_%H%M%S')
            self.init_workspace()
            self.tabWidget_init_windows()

    def init_workspace(self):
        path_list = [self.this_workspace_dir, self.this_workspace_dir+'/temp1', self.this_workspace_dir+'/temp2',
                     self.this_workspace_dir+'/work']
        for i in range(len(path_list)):
            if not os.path.exists(path_list[i]):
                os.mkdir(path_list[i])
